package io.kiki.sba.registry.common.model.slot.filter;

import com.google.common.base.Objects;
import io.kiki.sba.registry.common.model.constants.MultiValueConstants;
import io.kiki.sba.registry.common.model.store.DataInfo;
import lombok.NonNull;
import org.springframework.util.CollectionUtils;

import java.util.Set;

public class SyncPublisherGroupAcceptor implements SyncSlotAcceptor {

    private final String NAME = MultiValueConstants.SYNC_PUBLISHER_GROUP_ACCEPTOR;

    private final Set<String> acceptGroups;

    public SyncPublisherGroupAcceptor(Set<String> acceptGroups) {
        this.acceptGroups = acceptGroups;
    }

    @Override
    public boolean accept(@NonNull SyncAcceptorRequest request) {

        if (CollectionUtils.isEmpty(acceptGroups)) {
            return false;
        }
        DataInfo dataInfo = DataInfo.valueOf(request.getDataInfoId());
        return acceptGroups.contains(dataInfo.getGroup());
    }

    @Override
    public boolean filterOut(SyncAcceptorRequest request) {
        return false;
    }

    @Override
    public String name() {
        return NAME;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        SyncPublisherGroupAcceptor that = (SyncPublisherGroupAcceptor) o;
        return Objects.equal(NAME, that.NAME);
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(NAME);
    }


}
